package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wswdkl
 * @version 1.0
 * @date 2022/2/12 9:54
 */
@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {

    @Autowired
    private DamageListGoodsDao damageListGoodsDao;
    @Autowired
    private LogService logService;
    @Autowired
    private UserDao userDao;

    /**
     * 添加报损单
     * @param damageList
     * @param damageListGoodsStr
     * @return
     */
    @Override
    public boolean saveDamageListGoods(DamageList damageList, String damageListGoodsStr) {
        //1.先插入商品报损单
        damageListGoodsDao.saveDamageList(damageList);
        Integer damageListId = damageList.getDamageListId();
        //2.再插入商品报损列表
        List<DamageListGoods> damageListGoodsArrayList = JSONObject.parseArray(damageListGoodsStr,DamageListGoods.class);
        for (DamageListGoods damageListGoods : damageListGoodsArrayList) {
            damageListGoods.setDamageListId(damageListId);
        }
        damageListGoodsDao.saveDamageListGoods(damageListGoodsArrayList);
        logService.save(new Log(Log.UPDATE_ACTION,"保存报损单"));
        return true;
    }

    /**
     * 查询报损单
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> queryDamageList(String sTime, String eTime) {
        Map<String, Object> map = new HashMap<>();

        List<DamageList> damageLists = damageListGoodsDao.queryDamageList(sTime,eTime);
        map.put("rows",damageLists);

        for (DamageList damageList : damageLists) {
            User user = userDao.getUserById(damageList.getUserId());
            damageList.setTrueName(user.getTrueName());
        }

        logService.save(new Log(Log.SELECT_ACTION, "报损单查询"));
        return map;
    }

    /**
     * 查询报损单商品信息
     * @param damageListId
     * @return
     */
    @Override
    public Map<String, Object> queryDamageGoodsList(Integer damageListId) {
        Map<String, Object> map = new HashMap<>();

        List<DamageListGoods> damageGoodsList = damageListGoodsDao.queryDamageGoodsList(damageListId);
        map.put("rows",damageGoodsList);

        logService.save(new Log(Log.SELECT_ACTION, "报损单商品信息查询"));
        return map;
    }
}
